package Networking;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client {
	public static void main(String argv[]) throws Exception
	{
	   String webAddress;
	   int choice = 0;
	  // String finalSentence = "";
	   
	   Scanner input = new Scanner(System.in);	
	   System.out.println("Please enter a web page");
	   //assume they are competent 
	   
	   webAddress =  input.nextLine();
	   
	   System.out.println("Please select from following options "
			 //Client connects to the Web server W on port 80 and sends a correctly formatted simple HTTP Get request
			 //It then prints the data sent by W on the screen and closes the connection to W.
			 + "\n 1 - Connect to web server"
			 //The client connects to the "localhost" server L on port 12337 and sends the Web server name W to L. It then closes the connection to L.
	   		+ "\n 2 - Connect to local host");
	   
	 
	   //why not
	   choice = valInput(input);
	   
	   switch(choice){
	   case 1: 
		   accessWeb(webAddress);
		   break;
	   case 2: 
		   accessLocalHost(webAddress);
		   break;
	   }
	   
	} 
	   
	public static void accessWeb(String webAddress) throws Exception{
		String sentence;
		String modifiedSentence;
		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
	   
		//Create Socket
		Socket clientSocket = new Socket(webAddress, 80);
		
		//Set up our output stream so we can send things through the socket.
		DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
		
		//Set up our input stream so we can receive things through the socket.
		BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	   
		//Initialize the sentence that we're going to send out to the server.
		//Sentence is our message. In this case, it's a 'GET //' command. 
		sentence = "GET //";
		System.out.println(sentence);	
		
		//Convert our sentence to bytes and send it to the sever.
		outToServer.writeBytes(sentence+'\n');
		
		//Get and print whatever the server returns to us.
		System.out.print("From Server: ");
		while((modifiedSentence = inFromServer.readLine()) != null){
			System.out.println(modifiedSentence);
		}  
	   
		//Close the socket.
		clientSocket.close();
	}
	
	public static void accessLocalHost(String webAddress) throws Exception {
		
		//Create socket that connects to the local host.
		Socket clientSocket = new Socket("localhost", 12337);
		
		//Set up our output stream, which we will use to send -webAddress- to the localhost.
		DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
		
		//Send our message to the localhost.
		outToServer.writeBytes(webAddress);
		
		//Close the socket
		clientSocket.close();		
	}

	//validation for web page
	public static int valInput(Scanner input){
		int k = 0;
		k = input.nextInt();
		while(k != 1 && k != 2){
			System.out.println("ERROR, must pick either 1 or 2");
			k = input.nextInt();
		}
		return k; 
		
	}
}
